import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import store2 from 'store2'
import api from '@api'
import {wechatConfig} from '@utils'
Vue.use(Router)


// We are using Webpack code splitting here so that each route's associated
// component code is loaded on-demand only when the route is visited.
// It's actually not really necessary for a small project of this size but
// the goal is to demonstrate how to do it.
//
// Note that the dynamic import syntax should actually be just `import()`
// but buble/acorn doesn't support parsing that syntax until it's stage 4
// so we use the oldrequire here instead.
//
// If using Babel, `import()` can be supported via
// babel-plugin-syntax-dynamic-import.

const checkLogin = function(to, from, next) {
  if(store.state.global.user.isLogin) {
    next()
  } else {
    next('/login')
  }

}

const router = new Router({
  mode: 'history',
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  routes: [
    {
      path: '/',
      name: 'index',
      component: resolve => require(['../views/Index/index.vue'], resolve)
    },
    {
      path: '/shop/:shopId',
      name: 'shop',
      component: resolve => require(['../views/Shop/index.vue'], resolve)
    },
    {
      path: '/cate/:id',
      name: 'cate',
      component: resolve => require(['../views/Cate/index.vue'], resolve)
    },
    {
      path: '/fruit',
      name: 'fruit',
      component: resolve => require(['../views/Cate-old/fruit.vue'], resolve)
    },
    {
      path: '/login',
      name: 'login',
      component: resolve => require(['../views/Passport/login.vue'], resolve)
    },
    {
      path: '/register',
      name: 'register',
      component: resolve => require(['../views/Passport/register.vue'], resolve)
    },
    {
      path: '/forget',
      name: 'forget',
      component: resolve => require(['../views/Passport/forget.vue'], resolve)
    },
    {
      path: '/account',
      name: 'account',
      component: resolve => require(['../views/Account/index.vue'], resolve)
    },
    {
      path: '/setting',
      name: 'setting',
      component: resolve => require(['../views/Setting/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/goods/:id',
      name: 'goods',
      component: resolve => require(['../views/Shop/goodsDetail.vue'], resolve)
    },
    {
      path: '/setting/info',
      name: 'setInfo',
      component: resolve => require(['../views/Info/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/jifen',
      name: 'jifen',
      component: resolve => require(['../views/Jifen/index.vue'], resolve),
      beforeEnter: checkLogin,
      children: [
        {
          path: 'rule',
          name: 'jifen-rule',
          component: resolve => require(['../views/Jifen/rule.vue'], resolve)
        }
      ]
    },
    {
      path: '/coupon',
      name: 'coupon',
      redirect: {name:'coupon-list',params: {state: 0}},
      component: resolve => require(['../views/Coupon/index.vue'], resolve),
      beforeEnter: checkLogin,
      children: [
        {
          path: ':state',
          name: 'coupon-list',
          component: resolve => require(['../views/Coupon/list.vue'], resolve)
        }
      ]
    },
    {
      path: '/address',
      name: 'address',
      component: resolve => require(['../views/Address/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/address/add',
      name: 'address-add',
      component: resolve => require(['../views/Address/add.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/address/edit/:id',
      name: 'address-edit',
      component: resolve => require(['../views/Address/edit.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/message',
      name: 'message',
      component: resolve => require(['../views/Message/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/join',
      name: 'join',
      component: resolve => require(['../views/Join/index.vue'], resolve)
    },
    {
      path: '/tempOrder/:orderId',
      name: 'tempOrder',
       component: resolve =>require(['../views/TempOrder/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/choose/coupon',
      name: 'coupon-choose',
      component: resolve =>require(['../views/TempOrder/coupon.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/management',
      name: 'management',
      component: resolve =>require(['../views/Management/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/order',
      name: 'order',
      component: resolve =>require(['../views/Order/index.vue'], resolve),
    },
    {
      path: '/order/:id',
      name: 'orderDetail',
      component: resolve =>require(['../views/OrderDetail/index.vue'], resolve),
    },
    {
      path: '/rate/:id',
      name: 'rate',
      component: resolve =>require(['../views/Rate/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/pay/:orderId',
      name: 'pay',
      component: resolve =>require(['../views/Pay/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/success/:orderId',
      name: 'paySuccess',
      component: resolve =>require(['../views/PaySuccess/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/record',
      name: 'record',
      component: resolve =>require(['../views/Record/index.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/deposit',
      name: 'deposit',
      component: resolve =>require(['../views/Deposit/index.vue'], resolve),
      beforeEnter: checkLogin,
      children: [
        {
          path: 'rule',
          name: 'deposit-rule',
           component: resolve =>require(['../views/Deposit/rule.vue'], resolve),
        }
      ]
    },
    {
      path: '/520',
      name: '520hd',
      component: resolve =>require(['../views/520hd/index.vue'], resolve),
      beforeEnter: checkLogin,
      children: [
        {
          path: 'rule',
          name: '520hd-rule',
           component: resolve =>require(['../views/520hd/rule.vue'], resolve),
        }
      ]
    },
    {
      path: '/setting/pwd',
      name: 'setPwd',
      component: resolve =>require(['../views/Passport/change.vue'], resolve),
      beforeEnter: checkLogin
    },
    {
      path: '/ship',
      name: 'about',
      component: resolve =>require(['../views/About/index.vue'], resolve)
    },
    {
      path: '/renegade',
      name: 'renegade',
      component: resolve =>require(['../views/Renegade/index.vue'], resolve)
    },
    {
      path: '/renegade/share',
      name: 'renegadeShare',
      component: resolve =>require(['../views/Renegade/share.vue'], resolve)
    },
    {
      path: '/renegade/buy',
      name: 'renegadeBuy',
      component: resolve =>require(['../views/Renegade/buy.vue'], resolve)
    }
  ]
})
router.beforeEach( async(to, from, next) => {
  await store.dispatch('global/user/log');
  next()
})
router.afterEach(async(route) => {
  let u = navigator.userAgent;
  let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
  let url = isiOS ? store2('firsEnterUrl') : window.location.href.split('#')[0];
  await wechatConfig(location.href);
})
export default router
